import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'
import Layout from '@/components/layout/index.vue'

export const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { requiresAuth: false },
    hidden: true
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/index.vue'),
    meta: { requiresAuth: false },
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '仪表盘',
          icon: 'Odometer',
          keepAlive: true
        }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/user/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '用户管理',
          icon: 'User',
          roles: ['ADMIN'],
          keepAlive: true
        }
      },
      {
        path: 'areas',
        name: 'Areas',
        component: () => import('@/views/area/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '区域管理',
          icon: 'Location',
          roles: ['ADMIN'],
          keepAlive: true
        }
      },
      {
        path: 'points',
        name: 'Points',
        component: () => import('@/views/point/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '点位管理',
          icon: 'MapLocation',
          roles: ['ADMIN', 'OPERATOR'],
          keepAlive: true
        }
      },
      {
        path: 'machines',
        name: 'Machines',
        component: () => import('@/views/machine/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '售货机管理',
          icon: 'Box',
          roles: ['ADMIN', 'OPERATOR'],
          keepAlive: true
        }
      },
      {
        path: 'products',
        name: 'Products',
        component: () => import('@/views/product/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '商品管理',
          icon: 'Goods',
          roles: ['ADMIN', 'OPERATOR'],
          keepAlive: true
        }
      },
      {
        path: 'work-orders',
        name: 'WorkOrders',
        component: () => import('@/views/work-order/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '工单管理',
          icon: 'Document',
          roles: ['ADMIN', 'MAINTAINER'],
          keepAlive: true
        }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('@/views/order/index.vue'),
        meta: { 
          requiresAuth: true,
          title: '我的订单',
          icon: 'ShoppingCart',
          roles: ['CONSUMER'],
          keepAlive: true
        }
      },
      {
        path: 'profile',
        component: Layout,
        hidden: true,
        children: [
          {
            path: '',
            name: 'Profile',
            component: () => import('@/views/profile/index.vue'),
            meta: { title: '个人信息', icon: 'User' }
          }
        ]
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    hidden: true
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
  } else if (to.meta.roles && !to.meta.roles.includes(userStore.role)) {
    next('/dashboard') // 如果用户没有权限访问该页面，重定向到仪表盘
  } else {
    next()
  }
})

export default router 